package com.alibaba.citrus.cr.forecast.facade.service;

import com.alibaba.citrus.cr.common.annotation.FacadeInvoker;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.lattice2.epoch.middleware.mq.runtime.EpochMessageService;
import com.alibaba.lattice2.epoch.sdk.model.Result;

import com.epoch.app.crforecastsales.failorder.dto.ProcessRequest;
import com.epoch.app.crforecastsales.failorder.service.FailOrderService;
import com.epoch.app.crforecastsales.model.dto.FailOrderRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Author: jinghang
 * @Date: 2021/12/15
 * @Description:
 */
@Service
@Primary
public class FailOrderServiceImpl implements FailOrderService {
    private Log log = Log.getLogger(FailOrderServiceImpl.class);

    @Autowired
    private FailOrderProcessor failOrderProcessor;

    @Autowired
    private EpochMessageService messageService;


    @Override
    @FacadeInvoker
    public Result<Boolean> process(ProcessRequest request) {
        failOrderProcessor.process(request.getMessage(), null);
        return Result.success(true);
    }

    @Override
    @FacadeInvoker
    public Object sendFailMsg(FailOrderRequest failOrderRequest) {
        String message = JSON.toJSONString(failOrderRequest);
        Boolean result = messageService.sendMessage("SALE_ORDER_FAIL_MESSAGE", "CR_SALEORDER_V2", "SALE_CREATE_FAILED", message);
        if (Objects.nonNull(result) && !result) {
            log.error("FailOrderServiceImpl$sendFailMsg_fail");
        }
        return Result.success(true);
    }
}
